package com.jhws.business.comm.controller;

import com.alibaba.fastjson.JSON;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.comm.bo.Room;
import com.jhws.business.comm.bo.RoomTypeGroup;
import com.jhws.business.comm.bo.RoomTypeGroupBo;
import com.jhws.business.comm.service.RoomService;
import com.jhws.business.comm.service.RoomTypeGroupService;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/room")
public class RoomCtrl {
    @Resource
    private RoomService roomService;
    @Resource
    private RoomTypeGroupService roomTypeGroupService;

    /**
     * 保存房间信息
     *
     * @param room 房间信息
     */
    @RequestMapping("saveRoom.x")
    @ResponseBody
    public JsonRespData saveRoom(Room room) throws Exception {
        roomService.saveRoom(room);
        return new JsonRespData("", 1, "保存成功");
    }

    /**
     * 查询房间信息
     *
     * @param id 房间信息ID
     */
    @RequestMapping("queryRoom.x")
    @ResponseBody
    public JsonRespData queryRoom(String id) {
        return new JsonRespData("", 1, roomService.queryRoom(id));
    }

    /**
     * 条件查询房间信息列表
     */
    @RequestMapping("queryRooms.x")
    @ResponseBody
    public JsonRespData queryRooms() {
        String where = "where 1=1 ";
        return new JsonRespData("", 1, roomService.queryRooms(where));
    }

    /**
     * 分页查询房间信息列表
     *
     * @param param 条件
     */
    @RequestMapping("queryRoomPage.x")
    @ResponseBody
    public JsonRespData queryRoomPage(@RequestParam Map param) throws Exception {
        return new JsonRespData("", 1, roomService.queryRoomPage(param));
    }

    /**
     * 分配戶型
     *
     * @param roomId
     * @param roomTypeId
     */
    @RequestMapping("assignsRoomType.x")
    @ResponseBody
    public JsonRespData assignsRoomType(String roomId, long roomTypeId) {
        roomService.saveRoomType(roomId, roomTypeId);
        return new JsonRespData("", 1, "分配成功");
    }

    /**
     * 户型组分配戶型
     */
    @RequestMapping("assignsRoomTypeGroup.x")
    @ResponseBody
    public JsonRespData assignsRoomTypeGroup(String commCode, String buildingCode, String unitCode, String floorCode, long roomTypeGroupId) {
        List<Room> rooms = roomService.queryRooms("where comm_code='" + commCode + "' and building_code='" + buildingCode + "' and unit_code='" + unitCode + "' and floor_code='" + floorCode + "'");
        RoomTypeGroup roomTypeGroup = roomTypeGroupService.queryRoomTypeGroup(roomTypeGroupId);
        if (StringUtils.isEmpty(roomTypeGroup.getRoomTypeId())) {
            return new JsonRespData("", -1, "户型组没有分配户型！");
        }
        List<RoomTypeGroupBo> roomTypeGroupBos = JSON.parseArray(roomTypeGroup.getRoomTypeId(), RoomTypeGroupBo.class);
        if (rooms.size() != roomTypeGroupBos.size()) {
            return new JsonRespData("", -1, "户型组与户型数量不匹配！");
        }
        for (int i = 0; i < rooms.size(); i++) {
            roomService.saveRoomType(rooms.get(i).getId(), roomTypeGroupBos.get(i).getId());
        }
        return new JsonRespData("", 1, "分配成功");
    }

    /**
     * 删除房间信息
     *
     * @param room 房间信息ID
     */
    @RequestMapping("deleteRoom.x")
    @ResponseBody
    public JsonRespData deleteRoomType(Room room) {
        roomService.deleteRoom(room);
        return new JsonRespData("", 1, "删除成功");
    }
}
